import { Pear } from '@element-plus/icons';
import { Sort } from 'element-plus/es/components/table/src/table/defaults';
import { VxeGridProps } from 'vxe-table'

/** 查询参数：页数、排序 */
class QueryConfig{
    pageIndex:number;
    pageSize:number;
    sortName:string;
    sortOrder:string;

    constructor(pageIndex:number,pageSize:number,sortName:string="",sortOrder:string=""){
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }
}

//Java端定义的分页类
class Pager{
    paged:boolean = true;
    /** base 1 */
    pageIndex:number = 1;
    pageSize:number = 0;
    firstRow:number = 0;
    startRows:number = 0;
    endRows:number = 0;
    totalRows:number = 0;
    defaultPageSize:number = 10;
    pageSizeOptions=[10, 15, 20, 30, 50, 100, 500, 1000];
}

class EntityBO{
    data:any;
    codeTypes:any;
    nameTypes:any;
    authTag:number=0;
}

class ListBO{
    mainData:any;
    aggregations:any;
    items:any;
    pager:Pager|undefined;
    codeTypes:any;
    nameTypes:any;
    lockState:number=0;
    authTag:number=0;
}

/** 构造默认分页参数 */
function buildDefaultPager():Pager{
    let pager = new Pager();
    pager.pageSize = 10;
    pager.paged = true;
    pager.pageIndex = 1;
    pager.defaultPageSize = 10;
    pager.pageSizeOptions=[10, 15, 20, 30, 50, 100, 500, 1000];
    return pager;
}

class Sorter{
  field: string = ''; 
  order : string = '';
}

class DayuGridOptions{
    gridProps: VxeGridProps;
    pager : Pager;
    sorter : Sorter;

    constructor(gridProps: VxeGridProps,  pager : Pager, sorter : Sorter){
        this.gridProps = gridProps;
        this.pager = pager;
        this.sorter = sorter;
    }

    onSortChanged?:(field:string, order:string)=>void;

    changeSort(evt : any){// 使用方式： <vxe-grid @sortChange="($event:any)=>gridOptions.changeSort($event)"> 这种方式调用，才能使用this，获取到this.sorter等类变量。直接写 @sortChange="gridOptions.changeSort" 无法使用this
        debugger;
        let self = this;
        if(!self.sorter){
            self.sorter = new Sorter();
        }
        self.sorter.field = evt.property;//排序字段
        self.sorter.order = evt.order;//desc asc null

        if(!self.pager){
            self.pager = new Pager();
        }
        self.pager.pageIndex = 1;//重新设置到第一页
        
        if(self.onSortChanged){
            self.onSortChanged(self.sorter.field, self.sorter.order);
        }
    }
}

function buildDefaultVxeGridOptions(){
    
    let _gridProps: VxeGridProps =
    {
        border: true,
        resizable: true,
        height: '100%', //431： mini 10行 ，
        align: null,
        showHeaderOverflow: true,
        showOverflow: true,
        highlightHoverRow: true,
        highlightCurrentRow: true,
        size:'medium',
        seqConfig:{startIndex: 1},
        sortConfig:{remote: true, iconAsc: 'fa fa-chevron-up', iconDesc: 'fa fa-chevron-down'},
        toolbarConfig: {
            custom: true,
            slots: {
              buttons: 'toolbar_buttons'
            }
        }
    }

    let _pager = new Pager();
    _pager.pageSize = 10;
    _pager.paged = true;
    _pager.pageIndex = 1;
    _pager.defaultPageSize = 10;
    _pager.pageSizeOptions=[10, 15, 20, 30, 50, 100, 500, 1000];

    let _sorter = new Sorter();
    
    let options = new DayuGridOptions(_gridProps, _pager, _sorter);

    return options;
}

class ButtonProp{
    used:boolean=true;
    type:string='';
    text:string='';
    icon:any|undefined;
    onclick:()=>void=function(){};

    constructor(used:boolean){
        this.used = used;
    }
}


export {buildDefaultPager, buildDefaultVxeGridOptions, DayuGridOptions, QueryConfig, EntityBO, ListBO, Pager, ButtonProp};